﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GiMess
{
    public partial class IMForm : Form
    {
        private string toUsername;
        private string fullName;

        public IMForm()
        {
            InitializeComponent();
        }

        public IMForm(string name, string username)
        {
            InitializeComponent();
            this.Text = "Chating with: " + name + " (" + username + ")";
            toUsername = username;
            fullName = name;
        }

        public string IMUsername
        {
            get { return toUsername; }
        }

        public void AddIMText(string text)
        {
            textBox1.Invoke(new MethodInvoker(delegate() { textBox1.AppendText(fullName + ": " + text + "\r\n"); }));
            //textBox1.Text += fullName + ": " + text + "\r\n";
        }

        private void SendIM()
        {
            if (textBox2.Text != "")
            {
                textBox1.AppendText(AuthInfo.FirstName + " " + AuthInfo.LastName + ": " + textBox2.Text + "\r\n");
                GiNetClient.Instance.SendIM(toUsername, textBox2.Text);
                textBox2.Text = "";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendIM();
        }

        private void IMForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            FormsManager.RemoveIMForm(this);
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                SendIM();
            }
        }
    }
}
